/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.pavlospt.slice;

import com.github.pavlospt.CircleView;
import com.github.pavlospt.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * Main ability slice
 */
public class MainAbilitySlice extends AbilitySlice {
    /**
     * * The constant TAG_LOG
     */
    private static final String TAG_LOG = "MainAbilitySlice";
    /**
     * * The constant DOMAIN_ID
     */
    private static final int DOMAIN_ID = 0;
    /**
     * * The constant LABEL_LOG
     */
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, TAG_LOG);
    /**
     * * The constant LOG_FORMAT
     */
    private static final String LOG_FORMAT = "%{public}s: %{public}s";
    /**
     * The constant Circle view
     */
    private CircleView circleView;
    /**
     * The constant Tf main titletext
     */
    private TextField tf_main_titletext;
    /**
     * The constant Tf main titlesize
     */
    private TextField tf_main_titlesize;
    /**
     * The constant Tf main subtitletext
     */
    private TextField tf_main_subtitletext;
    /**
     * The constant Tf main subtitlesize
     */
    private TextField tf_main_subtitlesize;
    /**
     * The constant Btn main change
     */
    private Button btn_main_change;

    /**
     * On start *
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        circleView = (CircleView) findComponentById(ResourceTable.Id_cv_main_circleview);
        tf_main_titletext = (TextField) findComponentById(ResourceTable.Id_tf_main_titletext);
        tf_main_titlesize = (TextField) findComponentById(ResourceTable.Id_tf_main_titlesize);
        tf_main_subtitletext = (TextField) findComponentById(ResourceTable.Id_tf_main_subtitletext);
        tf_main_subtitlesize = (TextField) findComponentById(ResourceTable.Id_tf_main_subtitlesize);
        findComponentById(ResourceTable.Id_btn_main_change).setClickedListener(this::onClick);
    }

    /**
     * On click *
     *
     * @param component component
     */
    private void onClick(Component component) {
        circleView.setTitleText(tf_main_titletext.getText());
        String text = tf_main_titlesize.getText();
        try {
            circleView.setTitleSize(Integer.parseInt(text));
        } catch (NumberFormatException exception) {
            HiLog.error(LABEL_LOG, LOG_FORMAT, "circleView", "onClick text numberFormatException");
        }
        circleView.setSubtitleText(tf_main_subtitletext.getText());
        String text1 = tf_main_subtitlesize.getText();
        try {
            circleView.setSubtitleSize(Integer.parseInt(text1));
        } catch (NumberFormatException exception) {
            HiLog.error(LABEL_LOG, LOG_FORMAT, "circleView", "onClick text1 numberFormatException");
        }
    }

    /**
     * On active
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * On foreground *
     *
     * @param intent intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
